package com.lagou.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Activate(group = {CommonConstants.PROVIDER})
public class TransportIPFilter implements Filter {

    private static final Logger log = LoggerFactory.getLogger(TransportIPFilter.class);


    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        long   startTime  = System.currentTimeMillis();
        try {
            // 执行方法
            return  invoker.invoke(invocation);
        } finally {
            RpcContext context = RpcContext.getContext();
            String clientIp = context.getRemoteHost();
//            int remotePort = context.getRemotePort();
            log.info("请求的主机IP为：{}，请求的目标方法名为：{}",clientIp,invocation.getMethodName());
            log.info("处理请求共耗时：{}",(System.currentTimeMillis()-startTime) + "毫秒");
        }

    }
}
